<template>
  <div class="table-wrapper">
    <div class="search-container">
      <div class="search-wrapper">
        <span class="label">楼宇：</span>
        <el-cascader size="mini" class="large-select" v-model="roomIds" :options="buildingList" placeholder="请选择楼宇"
                     :props="{ checkStrictly: true, emitPath: false, multiple: true }"
                     collapse-tags
                     clearable
                     @expand-change="expandChange"
                     @change="geneParams">
        </el-cascader>
      </div>
<!--      <div class="search-wrapper">-->
<!--        <span class="label">房间：</span>-->
<!--        <el-cascader size="mini" class="large-select" v-model="params.room" :options="roomList" placeholder="请选择房间"-->
<!--                     :props="{ checkStrictly: true, emitPath: false, multiple: true }"-->
<!--                     collapse-tags-->
<!--                     clearable>-->
<!--        </el-cascader>-->
<!--      </div>-->
      <div class="search-wrapper">
        <span class="label">时间类型：</span>
        <el-select size="mini" v-model="params.time_type" placeholder="请选择时间类型" @change="changeTimeType">
          <el-option v-for="item in timeTypeList" :key="item.text" :label="item.text" :value="item.value"></el-option>
        </el-select>
      </div>
      <div class="search-wrapper">
        <span class="label">开始时间：</span>
        <el-date-picker
          size="mini"
          v-model="params.start_time"
          :type="params.time_type===1?'date':(params.time_type===2?'month':'year')"
          :value-format="params.time_type===1?'yyyy-MM-dd':(params.time_type===2?'yyyy-MM':'yyyy')"
          placeholder="选择开始时间"
        >
        </el-date-picker>
      </div>
      <div class="search-wrapper">
        <span class="label">结束时间：</span>
        <el-date-picker
          size="mini"
          v-model="params.end_time"
          :type="params.time_type===1?'date':(params.time_type===2?'month':'year')"
          :value-format="params.time_type===1?'yyyy-MM-dd':(params.time_type===2?'yyyy-MM':'yyyy')"
          placeholder="选择结束时间"
        >
        </el-date-picker>
      </div>
    </div>
    <div class="search-container">
      <div class="search-wrapper">
        <el-button @click="getFjChart" icon="el-icon-search" size="mini" >查询</el-button>
      </div>
    </div>
    <common-chart class="common-chart more-search" :chart-type="'line'" size="big" :data="chart" v-if="!loading"></common-chart>
  </div>
</template>

<script>
import CommonChart from '@/views/pages/components/CommonChart'
import portal from '@/api/portal'
export default {
  name: 'EnergyChartBuilding',
  props: ['type'],
  data () {
    return {
      loading: true,
      params: {
        page: 1,
        room: null,
        time_type: null,
        start_time: null,
        end_time: null
      },
      roomIds: [],
      buildingList: [],
      roomList: [],
      timeTypeList: [
        {
          text: '天',
          value: 1
        },
        {
          text: '月',
          value: 2
        },
        {
          text: '年',
          value: 3
        }
      ],
      data: [],
      chart: {
        name: '用电统计分析',
        xAxis: [{ data: [] }],
        series: [],
        legend: []
      },
      roomMap: {}
    }
  },
  components: {
    CommonChart
  },
  created () {
    this.getRoomClass()
  },
  computed: {
  },
  mounted () {
  },
  methods: {
    changeTimeType () {
      this.params.start_time = null
      this.params.end_time = null
    },
    async getFjChart () {
      if (!this.params.room) {
        this.$message.warning('请选择房间')
        return
      }
      if (!this.params.time_type) {
        this.$message.warning('请选择时间类型')
        return
      }
      if (!this.params.start_time) {
        this.$message.warning('请选择开始时间')
        return
      }
      if (!this.params.end_time) {
        this.$message.warning('请选择结束时间')
        return
      }
      const params = {
        room: this.params.room,
        time_type: this.params.time_type,
        start_time: this.params.start_time,
        end_time: this.params.end_time
      }
      const res = await portal.getFjChart(params)
      if (res.data) {
        this.loading = true
        const records = res.data
        const xAxisData = []
        const addressMap = {}
        records.forEach(e => {
          e.forEach((el, index) => {
            if (!index) {
              xAxisData.push(el.time)
            }
            if (!addressMap[el.name]) {
              addressMap[el.name] = []
            }
            // el.count = Math.ceil(Math.random() * 100)
            addressMap[el.name].push(el.count)
          })
        })
        const series = []
        const legend = []
        for (const key in addressMap) {
          legend.push(key)
          series.push(
            {
              type: 'line',
              name: key,
              barWidth: '12%',
              label: {
                show: true,
                position: 'top',
                textStyle: { color: '#fff', fontSize: 14, fontStyle: 'oblique' }
              },
              data: addressMap[key]
            }
          )
        }
        this.chart.series = series
        this.chart.xAxis = [
          { data: xAxisData }
        ]
        this.chart.legend = legend
        setTimeout(() => {
          this.loading = false
        })
      }
    },
    // 获取楼宇
    async getRoomClass () {
      const result = await portal.getRoomClass()
      if (result.data) {
        const building = result.data
        building.forEach(e => {
          e.label = e.name
          e.value = e.id
          e.disabled = true
          e.children = e.child
          if (e.children) {
            e.children.forEach(el => {
              el.label = el.name
              el.value = el.id
              el.disabled = true
              el.children = el.child || []
              // delete el.children
              // if (el.children) {
              //   el.children.forEach(ele => {
              //     ele.label = el.name
              //     ele.value = el.id
              //     delete ele.children
              //   })
              // }
            })
          }
        })
        this.buildingList = building
      }
    },
    async expandChange (val) {
      if (val && val.length === 2) {
        let building
        this.buildingList.forEach(e => {
          if (e.children) {
            e.children.forEach(el => {
              if (el.value === val[1]) {
                building = el
              }
            })
          }
        })
        if (!building.loaded) {
          const params = {
            id: val[1]
          }
          const result = await portal.getRoomClassName(params)
          if (result.data) {
            const rooms = result.data
            rooms.forEach(e => {
              e.label = e.name
              e.value = e.id
              e.$parent = building
              e.disabled = false
              this.roomMap[e.id] = e
            })
            building.children = rooms
            building.loaded = true
          }
        }
      }
    },
    // 获取房间
    async geneParams () {
      const params = []
      if (this.roomIds && this.roomIds.length) {
        this.roomIds.forEach(e => {
          params.push(this.roomMap[e].$parent.id + '-' + this.roomMap[e].room_no)
        })
      }
      this.params.room = params.join('@')
    }
  }
}
</script>

<style scoped lang="scss">

</style>
